import { h } from "vue";
import { ElNotification, ElMessage, ElMessageBox } from "element-plus";
import axios from "axios";
import dayjs from "dayjs";
import pinia from "@/store/index.js";
import useJournalStore from "@/store/journal.js";

const journalStore = useJournalStore(pinia);

export default function () {
  const rateAlertValue = journalStore.flowNote.rateAlertValue;
  {
    axios
      .get("api/note_flow_query.php")
      .then((response) => {
        console.log("loadRecentFlowNote", response.data);
        if (response.data.success) {
          journalStore.flowNote.recentNote.content = response.data.result;
          if (
            response.data.result.length == 1 &&
            response.data.result[0].number !=
              journalStore.flowNote.recentNote.number
          ) {
            let message = new Array();
            if (response.data.result[0].manualDifference > 0) {
              // || response.data.result[0].adviceDifference > 0
              message.push(
                "建议额外增开 " +
                  Math.max(
                    response.data.result[0].manualDifference,
                    response.data.result[0].adviceDifference
                  ) +
                  " 个扇区！"
              );
            }
            if (response.data.result[0].rateZUUU < rateAlertValue) {
              message.push("请关注双流机场正常率！");
            }
            if (response.data.result[0].rateZUTF < rateAlertValue) {
              message.push("请关注天府机场正常率！");
            }
            if (message.length) {
              ElMessageBox({
                type: "warning",
                title: "流量与正常率记录",
                message: h(
                  "div",
                  null,
                  message.map((msg) => h("p", null, msg))
                ),
                closeOnClickModal: false,
                closeOnPressEscape: false,
                showClose: false,
              }).then(() => {
                journalStore.flowNote.recentNote.number =
                  response.data.result[0].number;
              });
            }
          }
          // ElMessage({
          //   message: `查找到${response.data.result.length}条流量与正常率记录`,
          //   type: "success",
          // });
        } else {
          // ElMessage({
          //   message: "未查找到任何流量与正常率记录",
          //   type: "warning",
          // });
        }
      })
      .catch((error) => {
        console.error("loadRecentFlowNote", error.message);
        // ElMessage({
        //   message: error.message,
        //   type: "warning",
        // });
      });
  }
}
